using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml.Serialization;
using System.Xml;
using System.Windows.Forms;

namespace Trebuchet
{
    public class ShortcutCollection : List<Shortcut>
    {
        public enum ShortcutType
        {
            Cache,
            StartMenu,
            User
        }
        private ShortcutType type;

        public ShortcutType Type
        {
            get { return type; }
            set { type = value; }
        }

        private static string GetFilename(ShortcutType type)
        {
            string filename = string.Empty;
            switch (type)
            {
                case ShortcutType.Cache:
                    filename = "CachedShortcuts.xml";
                    break;
                case ShortcutType.StartMenu:
                    filename = "StartMenuShortcuts.xml";
                    break;
                case ShortcutType.User:
                    filename = "UserShortcuts.xml";
                    break;
                default:
                    break;
            }
            return filename;
        }

        public void Save()
        {
            string filename = string.Format("{0}\\{1}", Application.LocalUserAppDataPath, GetFilename(type));
            MemoryStream stream = new MemoryStream();
            XmlSerializer xs = new XmlSerializer(this.GetType());
            XmlTextWriter writer = new XmlTextWriter(stream, Encoding.UTF8);
            xs.Serialize(writer, this);
            stream = (MemoryStream)writer.BaseStream;

            UTF8Encoding encoding = new UTF8Encoding();
            string returnString = encoding.GetString(stream.ToArray());
            TextWriter textWriter = new StreamWriter(filename);
            textWriter.WriteLine(returnString);
            textWriter.Close();           
        }

        public static ShortcutCollection Read(ShortcutType type)
        {
            string filename = string.Format("{0}\\{1}", Application.LocalUserAppDataPath, GetFilename(type));
            if (File.Exists(filename))
            {
                try
                {
                    TextReader reader = new StreamReader(filename);
                    string xml = reader.ReadToEnd();
                    reader.Close();

                    XmlSerializer xs = new XmlSerializer(typeof(ShortcutCollection));
                    UTF8Encoding encoding = new UTF8Encoding();
                    byte[] byteArray = encoding.GetBytes(xml);
                    MemoryStream stream = new MemoryStream(byteArray);
                    XmlTextWriter writer = new XmlTextWriter(stream, Encoding.UTF8);
                    Object myObject = xs.Deserialize(stream);
                    ShortcutCollection shortcuts = (ShortcutCollection)myObject;
                    shortcuts.Type = type;
                    return shortcuts;
                }
                catch
                {
                    return new ShortcutCollection();
                }
            }
            else
            {
                return new ShortcutCollection();
            }
        }

        internal Shortcut GetShortcutByName(string selectedName)
        {
            for (int i = 0; i < this.Count; i++)
            {
                if (this[i].Name == selectedName)
                    return this[i];
            }
            return null;
        }
    }
}
